package com.prs.crm.action.cqunicom;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.annotation.Clew;
import com.prs.crm.context.AppContext;
import com.prs.crm.domain.cqunicom.Company;
import com.prs.crm.domain.cqunicom.LogSerch;
import com.prs.crm.domain.cqunicom.NoteContent;
import com.prs.crm.domain.cqunicom.NoteEditor;
import com.prs.crm.domain.cqunicom.NoteInformation;
import com.prs.crm.domain.cqunicom.NoteNumber;
import com.prs.crm.domain.cqunicom.PoplarOrder;
import com.prs.crm.domain.cqunicom.Services;
import com.prs.crm.domain.cqunicom.ServicesType;
import com.prs.crm.service.cqunicom.CompanyService;
import com.prs.crm.service.cqunicom.LogSerchService;
import com.prs.crm.service.cqunicom.NoteContentService;
import com.prs.crm.service.cqunicom.NoteEditorService;
import com.prs.crm.service.cqunicom.NoteInformationService;
import com.prs.crm.service.cqunicom.NoteNumberService;
import com.prs.crm.service.cqunicom.OrderService;
import com.prs.crm.service.cqunicom.ServicesService;
import com.prs.crm.service.cqunicom.ServicesTypeService;

/**
 * @author LanXiaoGang
 * @E-mail:lan9liang@163.com
 * @qq:7857553326
 * @version 创建时间：2011-3-17 下午04:24:59 简单说明
 */

public class NoteInformationAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private PaginationSupport pagination;

	private PoplarOrder order;
    private String word;
	private NoteEditor noteEditor;
	private LogSerchService logSerchService;
	private NoteEditorService noteEditorService;

	private NoteContent noteContent;

	private NoteContentService noteContentService;

	private NoteInformation noteInformation;

	private NoteNumber noteNumber;

	private List<NoteNumber> numList;

	private List<NoteContent> conList;

	private NoteInformationService noteInformationService;

	private NoteNumberService noteNumberService;

	private OrderService orderService;

	private Services services;
	
	private Company company;
	
	private List<Company>  viewComLIst=new ArrayList<Company>();
	
	private CompanyService companyService;
	
	private String veryfyNum ;
	
    private String rowdata;

	private List<Services> listServices;

	private ServicesService servicesService;

	private ServicesType servicesType;

	private ServicesTypeService servicesTypeService;

	@Action("editOperationNote")
	public String editOperationNote() {
		listServices = this.getServicesService().getByall();

		return SUCCESS;
	}
    /**
    * 序列号查询
    * @return
     */
	
	@Action(value = "verifySerizd", results = { @Result(name = "success", type = "json", params = {
			"root", "rowdata" }) })
	public String verifySerizd() {
		//System.out.println("序列号："+veryfyNum);
		Company  com=this.getCompanyService().getCompanyBySerialNum(veryfyNum);
		if(com==null){
			this.rowdata="A";
		}else{
			this.rowdata=com.getId()+"";
		}
		
		return SUCCESS;
	}
	@Action("listOperationNote")
	public String listOperationNote() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				NoteEditor.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getNoteEditorService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("listNoteInfor")
	public String listNoteInfor() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				NoteNumber.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getNoteNumberService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("listNoteContent")
	public String listNoteContent() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				NoteContent.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getNoteContentService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	
	@Action("listNoteInformation")
	public String listNoteInformation() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				NoteInformation.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getNoteInformationService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("editNoteInfor")
	public String editNoteInfor() {

		return SUCCESS;
	}

	@Action("editSMSSend")
	public String editSMSSend() {
		numList = this.getNoteNumberService().getAllNoteNumber();
		conList = this.getNoteContentService().getAllNoteContent();
		return SUCCESS;
	}

	@Action("lookNoteContent")
	public String lookNoteContent() {

		return SUCCESS;
	}

	@Action("editNoteContent")
	public String editNoteContent() {

		return SUCCESS;
	}

	@Action("lookNoteNumber")
	public String lookNoteNumber() {

		return SUCCESS;
	}
	@Action("lookNoteInformation")
	public String lookNoteInformation() {

		return SUCCESS;
	}

	@Action("lookNoteEditor")
	public String lookNoteEditor() {
		listServices = this.getServicesService().getAllServices();
		NoteEditor  NE=this.getNoteEditorService().get(noteEditor.getId());
		String serial[]=(NE.getSerialName()).split(",");
		for(String o:serial){
			if("".equals(o)|| o==null){
				
			}else{
				Company d=this.getCompanyService().get(Integer.parseInt(o));
				viewComLIst.add(d)	;
			}
		}
		return SUCCESS;
	}

	/**
	 * 新增业务短信消息
	 * 
	 * @return
	 */
	@Clew(results = { "success", "none" })
	@Action(value = "saveedittor", results = {
			@Result(name = "success", type = "redirect", location = "editOperationNote"),
			@Result(name = "none", type = "redirect", location = "editOperationNote"),
			@Result(name = "error", type = "chain", location = "editOperationNote") })
	public String saveedittor() {
		
		LogSerch  log=new LogSerch();
		log.setCandlePeron(AppContext.getAccountContext().currentUser().getUserName());
		log.setCandleContents("新增了业务短信消息:"+noteEditor.getSerialName());
		this.getLogSerchService().save(log);
		Services ss=this.getServicesService().get(noteEditor.getServicestor().getId());
		ss.setNoteHand("2");
		this.getServicesService().saveOrUpservices(ss);
		this.getNoteEditorService().save(noteEditor);
		this.setPrompt("数据处理中...");
		return SUCCESS;
	}

	/**
	 * 修改业务短信消息
	 * 
	 * @return
	 */
	@Clew(results = { "success", "none" })
	@Action(value = "UpdateEdittor", results = {
			@Result(name = "success", type = "redirect", location = "lookNoteEditor"),
			@Result(name = "none", type = "redirect", location = "lookNoteEditor"),
			@Result(name = "error", type = "chain", location = "lookNoteEditor") })
	public String UpdateEdittor() {
		NoteEditor ne = this.getNoteEditorService().get(noteEditor.getId());
		ne.setModifyTime(new Date());
		ne.setServerTitle(noteEditor.getServerTitle());
		ne.setSerialName(noteEditor.getSerialName());
		ne.setServicestor(noteEditor.getServicestor());
		ne.setYesNohandle(noteEditor.getYesNohandle());
		ne.setInforMo(noteEditor.getInforMo());
		this.getNoteEditorService().save(ne);
		this.setPrompt("数据处理中...");
		return SUCCESS;
	}

	/**
	 * 短信号码
	 * 
	 * @return
	 */
	@Clew(results = { "success", "none" })
	@Action(value = "saveeditInfor", results = {
			@Result(name = "success", type = "redirect", location = "editNoteInfor"),
			@Result(name = "none", type = "redirect", location = "editNoteInfor"),
			@Result(name = "error", type = "chain", location = "editNoteInfor") })
	public String saveeditInfor() {
		LogSerch  log=new LogSerch();
		log.setCandlePeron(AppContext.getAccountContext().currentUser().getUserName());
		log.setCandleContents("新加了短信号码");
		this.getLogSerchService().save(log);
		this.getNoteNumberService().save(noteNumber);
		this.setPrompt("数据处理中...");
		return SUCCESS;
	}

	/**
	 * 短信号码
	 * 
	 * @return
	 */
	@Clew(results = { "success", "none" })
	@Action(value = "saveUpdateNumber", results = {
			@Result(name = "success", type = "redirect", location = "listNoteInfor"),
			@Result(name = "none", type = "redirect", location = "listNoteInfor"),
			@Result(name = "error", type = "chain", location = "listNoteInfor") })
	public String saveUpdateNumber() {
		NoteNumber nn = this.getNoteNumberService().get(noteNumber.getId());
		nn.setModifyTime(new Date());
		nn.setNoteTitle(noteNumber.getNoteTitle());
		nn.setNoteRemark(noteNumber.getNoteRemark());
		this.getNoteNumberService().submit(nn);
		this.setPrompt("数据处理中...");
		return SUCCESS;
	}

	/**
	 * 短信内容
	 * 
	 * @return
	 */
	@Clew(results = { "success", "none" })
	@Action(value = "savenoteContent", results = {
			@Result(name = "success", type = "redirect", location = "listNoteContent"),
			@Result(name = "none", type = "redirect", location = "listNoteContent"),
			@Result(name = "error", type = "chain", location = "listNoteContent") })
	public String savenoteContent() {
		LogSerch  log=new LogSerch();
		log.setCandlePeron(AppContext.getAccountContext().currentUser().getUserName());
		log.setCandleContents("新增了短信内容");
		this.getLogSerchService().save(log);
		this.getNoteContentService().save(noteContent);
		this.setPrompt("数据处理中...");
		return SUCCESS;
	}

	/**
	 * 短信内容
	 * 
	 * @return
	 */
	@Clew(results = { "success", "none" })
	@Action(value = "saveUpdateNoteContent", results = {
			@Result(name = "success", type = "redirect", location = "listNoteContent"),
			@Result(name = "none", type = "redirect", location = "listNoteContent"),
			@Result(name = "error", type = "chain", location = "listNoteContent") })
	public String saveUpdateNoteContent() {
		NoteContent nn = this.getNoteContentService().get(noteContent.getId());
		nn.setModifyTime(new Date());
		nn.setContentTitle(noteContent.getContentTitle());
		nn.setContentRemark(noteContent.getContentRemark());
		this.getNoteContentService().submit(nn);
		this.setPrompt("数据处理中...");
		return SUCCESS;
	}

	/**
	 * 短信发送
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 * @throws MalformedURLException
	 */
	@Clew(results = { "success", "none" })
	@Action(value = "saveSendInformation", results = {
			@Result(name = "success", type = "redirect", location = "editSMSSend"),
			@Result(name = "none", type = "redirect", location = "editSMSSend"),
			@Result(name = "error", type = "chain", location = "editSMSSend") })
	public String saveSendInformation() throws MalformedURLException,
			UnsupportedEncodingException {

		LogSerch  log=new LogSerch();
		log.setCandlePeron(AppContext.getAccountContext().currentUser().getUserName());
		log.setCandleContents("进行了短信的发送操作");
		this.getLogSerchService().save(log);
		
		
		String telNum=noteInformation.getNoteNumber();
		
		String tes[]=telNum.split(",");
		for(String ss:tes){
			if(!"".equals(ss) && ss!=null){
			sendSMS4Other(ss, new Date()+"", noteInformation.getNoteContent());
			}
		}
		noteInformation.setPersonInfor(AppContext.getAccountContext().currentUser());
		noteInformation.setNoteTitle(new Date()+"");
        this.getNoteInformationService().save(noteInformation);
		this.setPrompt("数据处理中...");
		return SUCCESS;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public PoplarOrder getOrder() {
		return order;
	}

	public void setOrder(PoplarOrder order) {
		this.order = order;
	}

	public OrderService getOrderService() {
		return orderService;
	}

	public void setOrderService(OrderService orderService) {
		this.orderService = orderService;
	}

	public Services getServices() {
		return services;
	}

	public void setServices(Services services) {
		this.services = services;
	}

	public ServicesService getServicesService() {
		return servicesService;
	}

	public void setServicesService(ServicesService servicesService) {
		this.servicesService = servicesService;
	}

	public ServicesType getServicesType() {
		return servicesType;
	}

	public void setServicesType(ServicesType servicesType) {
		this.servicesType = servicesType;
	}

	public ServicesTypeService getServicesTypeService() {
		return servicesTypeService;
	}

	public void setServicesTypeService(ServicesTypeService servicesTypeService) {
		this.servicesTypeService = servicesTypeService;
	}

	public List<Services> getListServices() {
		return listServices;
	}

	public void setListServices(List<Services> listServices) {
		this.listServices = listServices;
	}

	public NoteEditor getNoteEditor() {
		return noteEditor;
	}

	public void setNoteEditor(NoteEditor noteEditor) {
		this.noteEditor = noteEditor;
	}

	public NoteEditorService getNoteEditorService() {
		return noteEditorService;
	}

	public void setNoteEditorService(NoteEditorService noteEditorService) {
		this.noteEditorService = noteEditorService;
	}

	public NoteContent getNoteContent() {
		return noteContent;
	}

	public void setNoteContent(NoteContent noteContent) {
		this.noteContent = noteContent;
	}

	public NoteContentService getNoteContentService() {
		return noteContentService;
	}

	public void setNoteContentService(NoteContentService noteContentService) {
		this.noteContentService = noteContentService;
	}

	public NoteInformation getNoteInformation() {
		return noteInformation;
	}

	public void setNoteInformation(NoteInformation noteInformation) {
		this.noteInformation = noteInformation;
	}

	public NoteNumber getNoteNumber() {
		return noteNumber;
	}

	public void setNoteNumber(NoteNumber noteNumber) {
		this.noteNumber = noteNumber;
	}

	public NoteInformationService getNoteInformationService() {
		return noteInformationService;
	}

	public void setNoteInformationService(
			NoteInformationService noteInformationService) {
		this.noteInformationService = noteInformationService;
	}

	public NoteNumberService getNoteNumberService() {
		return noteNumberService;
	}

	public void setNoteNumberService(NoteNumberService noteNumberService) {
		this.noteNumberService = noteNumberService;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public List<NoteNumber> getNumList() {
		return numList;
	}

	public void setNumList(List<NoteNumber> numList) {
		this.numList = numList;
	}

	public List<NoteContent> getConList() {
		return conList;
	}

	public void setConList(List<NoteContent> conList) {
		this.conList = conList;
	}

	/*
	 * 短信發送接口 发送方法 其他方法同理 输入参数： CorpID-帐号， Pwd-密码， Mobile-发送手机号， Content-发送内容，
	 * Cell-子号(可为空），
	 * SendTime-定时发送时间(固定14位长度字符串，比如：20060912152435代表2006年9月12日15时24分35秒，可为空)
	 * 输出参数：整数 0，发送成功； -1、帐号未注册； -2、其他错误； -3、密码错误； -4、手机号格式不对； -5、余额不足；
	 * -6、定时发送时间不是有效的时间格式； 注意：禁止10小时以内向同一手机号发送相同短信；
	 */

	private static int sendSMS4Other(String Mobile, String Content,
			String send_time) throws MalformedURLException,
			UnsupportedEncodingException {
		ResourceBundle rb = ResourceBundle.getBundle("note");
		URL url = null;
		String CorpID = rb.getString("userName");// 账户名
		String Pwd = rb.getString("password");// 密码
		String send_content = URLEncoder.encode(
				Content.replaceAll("<br/>", " "), "GBK");// 发送内容
		url = new URL("http://mb345.com/WS/Send.aspx?CorpID=" + CorpID
				+ "&Pwd=" + Pwd + "&Mobile=" + Mobile + "&Content="
				+ send_content + "&Cell=&SendTime=" + send_time);
		BufferedReader in = null;
		int inputLine = 0;
		try {
			//System.out.println("开始发送短信手机号码为 ：" + Mobile);
			in = new BufferedReader(new InputStreamReader(url.openStream()));
			inputLine = new Integer(in.readLine()).intValue();
		} catch (Exception e) {
			//System.out.println("网络异常,发送短信失败！");
			inputLine = -9;
		}
		//System.out.println("结束发送短信返回值：  " + inputLine);
		return inputLine;
	}

	public Company getCompany() {
		return company;
	}

	public void setCompany(Company company) {
		this.company = company;
	}

	public CompanyService getCompanyService() {
		return companyService;
	}

	public void setCompanyService(CompanyService companyService) {
		this.companyService = companyService;
	}

	public String getVeryfyNum() {
		return veryfyNum;
	}

	public void setVeryfyNum(String veryfyNum) {
		this.veryfyNum = veryfyNum;
	}

	public String getRowdata() {
		return rowdata;
	}

	public void setRowdata(String rowdata) {
		this.rowdata = rowdata;
	}
	public LogSerchService getLogSerchService() {
		return logSerchService;
	}
	public void setLogSerchService(LogSerchService logSerchService) {
		this.logSerchService = logSerchService;
	}
	public List<Company> getViewComLIst() {
		return viewComLIst;
	}
	public void setViewComLIst(List<Company> viewComLIst) {
		this.viewComLIst = viewComLIst;
	}
	public String getWord() {
		return word;
	}
	public void setWord(String word) {
		this.word = word;
	}
	
}
